package de.hska.pwmanager.controller;

import static de.hska.pwmanager.validation.ValidationUtils.isNullOrEmpty;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import de.hska.pwmanager.controller.errors.ValidationError;
import de.hska.pwmanager.controller.errors.ValidationException;
import de.hska.pwmanager.persistence.model.PasswordData;
import de.hska.pwmanager.persistence.model.User;
import de.hska.pwmanager.persistence.model.dao.PasswordDataDao;
import de.hska.pwmanager.persistence.model.dao.PasswordUserHashDao;
import de.hska.pwmanager.persistence.model.dao.UserDao;

@Controller
@RequestMapping(value = "users")
public class UserController extends AbstractController implements IUserController {
	@Autowired
	private UserDao userDao;
	@Autowired
	private PasswordDataDao passwordDataDao;
	@Autowired
	private PasswordUserHashDao passwordUserHashDao;
	@Autowired
	private PasswordController passwordController;

	public User prototype(int param) {
		return (new User().setId(123L).setPassword("123").setPubkey("helo")
				.setUsername("abc"));
	}

	public List<User> readAll() {
		return this.userDao.readAll();
	}

	@RequestMapping("/test")
	public void test() throws Exception {
		throw new ValidationException();
	}
	

	public void createOrUpdate(@RequestBody User toCreateOrUpdate)
			throws ValidationException {
		this.validate(toCreateOrUpdate);
		// is it a user edit and the password is empty - admin doenst do a pw reset
		if (toCreateOrUpdate.getId()!=null && toCreateOrUpdate.getPassword() == null) {
			User userInDB = this.userDao.readByPrimaryKey(toCreateOrUpdate
					.getId());
			toCreateOrUpdate.setPassword(userInDB.getPassword());
		}
		this.userDao.save(toCreateOrUpdate);
	}

	public void delete(@PathVariable(value = "id") Long pkToDelete) {
		User toDelete = this.userDao.readByPrimaryKey(pkToDelete);
		for (PasswordData passwordData : this.passwordController
				.getUserPasswords(toDelete)) {
			PasswordData fullEntry = this.passwordDataDao
					.readByPrimaryKey(passwordData.getId());
			if (fullEntry.getPasswordUserHashs().size() > 1) {
				fullEntry.getPasswordUserHashs().remove(
						passwordData.getPasswordUserHashs().get(0));
				this.passwordDataDao.save(fullEntry);
				this.passwordUserHashDao.delete(passwordData
						.getPasswordUserHashs().get(0));
			} else {
				this.passwordDataDao.delete(fullEntry);
			}
		}
		this.userDao.delete(toDelete);
	}

	public void validate(User toValidate) throws ValidationException {
		ValidationException exception = new ValidationException();
		if (isNullOrEmpty(toValidate.getUsername())) {
			exception.addError(new ValidationError("username",
					"Username field can't be empty"));
		} else {
			if(toValidate.getId()==null&& this.userDao.findByUsername(toValidate.getUsername()) !=null) {
				exception.addError(new ValidationError("username",
				"Already a user with that names exists"));
			}
		}
		if(toValidate.getId()==null && isNullOrEmpty(toValidate.getPassword())) {
			exception.addError(new ValidationError("password",
			"Password field can't be empty"));
		}
		
		exception.throwMe();
	}

	public User readByPK(@PathVariable(value = "id") Long pk) {
		return this.userDao.readByPrimaryKey(pk);
	}

}
